package cn.appsys.controller.backend;

import cn.appsys.pojo.*;
import cn.appsys.service.backend.UserLoginService;
import cn.appsys.service.developer.DevUserService;
import cn.appsys.tools.Constants;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserLoginController {
    @Resource
 private UserLoginService userLoginService;
    @Resource
    private DevUserService devUserService;
    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/login")
    public  String login(){
System.out.println("hello>>>>>>>>>>>>>>>");
        return "backendlogin";
    }

    /**
     * 登录
     * @param userCode
     * @param userPassword
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value="/dologin",method=RequestMethod.POST)
    public String doLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session){

        BackendUser backendUser=userLoginService.checkLogin(userCode,userPassword);
        System.out.println("hello>>>"+backendUser);
        if (backendUser!=null){
            session.setAttribute(Constants.USER_SESSION,backendUser);
            return "backend/main";
        }else{
            //页面跳转（login.jsp）带出提示信息--转发
            request.setAttribute("error", "用户名或密码不正确");
            return "backendlogin";
        }
    }
    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping(value="/logout")
    public  String logout(HttpSession session){
              session.removeAttribute(Constants.USER_SESSION);
        return "backendlogin";
    }


}
